package com.qef.com.mmovie.mvp.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.avos.avoscloud.AVObject;
import com.qef.com.mmovie.App;
import com.qef.com.mmovie.R;
import com.qef.com.mmovie.base.BaseRecycerViewAdapter;
import com.qef.com.mmovie.entity.DataBean;
import com.qef.com.mmovie.entity.LaughImgEntity;
import com.qef.com.mmovie.entity.VVIdeoDetailsEntity;
import com.qef.com.mmovie.img.ImageLoader;
import com.qef.com.mmovie.utils.Constants;
import com.qef.com.mmovie.utils.DateUtil;
import com.qef.com.mmovie.utils.GsonUtils;
import com.qef.com.mmovie.utils.SystemUtil;

import java.util.List;

/**
 * 收藏页面adapter
 */
public class MyCommentedAdapter extends BaseRecycerViewAdapter<AVObject, MyCommentedAdapter.Myholder> {
    public MyCommentedAdapter(Context context, List<AVObject> list) {
        super(context, list);
    }

    @Override
    public Myholder getCreateViewHolder(ViewGroup parent, int viewType) {
        return new Myholder(View.inflate(App.getInstance(), R.layout.item_commented, null));
    }

    @Override
    public void getBindViewHolder(Myholder holder, int position) {
        String title = null;
        String imagHead = null;
        String category = null;
        long durition = 0;
        if (Integer.parseInt(list.get(position).get(Constants.MyComment.VIDEO_TYPE).toString()) == 1) {
            DataBean dataBean = (DataBean) GsonUtils.
                    jsonToBean(list.get(position).get(Constants.Collection.VIDEO_INFO).toString(), DataBean.class);
            title = dataBean.getTitle();
            imagHead = dataBean.getCover().getDetail();
            category = dataBean.getCategory();
            durition = dataBean.getDuration();
        } else if (Integer.parseInt(list.get(position).get(Constants.MyComment.VIDEO_TYPE).toString()) == 3) {
            VVIdeoDetailsEntity dataEntity = (VVIdeoDetailsEntity) GsonUtils.
                    jsonToBean(list.get(position).get(Constants.Collection.VIDEO_INFO).toString(),
                            VVIdeoDetailsEntity.class);
            title = dataEntity.getData().getTitle();
            imagHead = dataEntity.getData().getImage();
            category = dataEntity.getData().getCate().get(0);
            durition = Long.parseLong(dataEntity.getData().getContent().getVideo().get(0).getDuration());
            ;
        }
        ImageLoader.getInstance().displayImage(App.getInstance(), imagHead,
                holder.ivVideo, SystemUtil.dp2px(200), SystemUtil.dp2px(110));
        holder.tvTitle.setText(title);
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("#").append(category)
                .append(" ")
                .append(" / ")
                .append(" ")
                .append(DateUtil.formatTime2(durition));
        holder.tvDescription.setText(stringBuilder.toString());
        holder.tv_comment_content.setText(list.get(position).get(Constants.MyComment.COMMENTCONTENT).toString());
        holder.itemView.setOnClickListener(v -> {
            if (mClickListener != null) {
                mClickListener.onItemClick(position, v, holder);
            }
        });

    }

    class Myholder extends RecyclerView.ViewHolder {
        ImageView ivVideo;
        TextView tvTitle;
        TextView tvDescription;
        TextView tv_comment_content;

        public Myholder(View itemView) {
            super(itemView);
            ivVideo = (ImageView) itemView.findViewById(R.id.iv_video);
            tvTitle = (TextView) itemView.findViewById(R.id.title);
            tvDescription = (TextView) itemView.findViewById(R.id.description);
            tv_comment_content = (TextView) itemView.findViewById(R.id.tv_comment_content);
        }
    }
}
